package com.kaishengit.service;

import java.io.FileInputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.DealDao;
import com.kaishengit.pojo.Contacts;
import com.kaishengit.pojo.Deal;
import com.kaishengit.pojo.User;
import com.kaishengit.util.DateUtil;
@Transactional
public class DealService {
	private DealDao dealDao;

	
	public List<Deal> find() {
		return dealDao.findAll();
	
	}
	
	
	public void save(Deal deal) {
		dealDao.save(deal);
	}
	

	public void dele(String id) {
		dealDao.Del(id);
		
	}
	
	public Deal findById(String id) {
		return dealDao.get(id);
	
	}
	
	public void update(Deal deal) {
		dealDao.save(deal);
	}

	//get set
	public DealDao getDealDao() {
		return dealDao;
	}

	public void setDealDao(DealDao dealDao) {
		this.dealDao = dealDao;
	}


	public void new_chance(Deal deal, Contacts contacts, String view,
			String statetype, String grouptype, User user) {
		if(grouptype != null) {
			if("grouptype".equals(view)) {
				view = grouptype;
			}
		}
		deal.setCreatetime(DateUtil.getNow());
		deal.setContacts(contacts);
		deal.setUser(user);
		deal.setState(statetype);
		deal.setView(view);
		dealDao.save(deal);
	}

	public void import_dealList(String import_content, User user) {
		Workbook wb = null;
		try {
			wb = new HSSFWorkbook(new FileInputStream("C:/import/"+import_content+".xls"));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		Sheet st = wb.getSheetAt(0);
		for (int i = 1; i <= st.getLastRowNum(); i++) {
				Row row = st.getRow(i);
				Deal deal = new Deal();
				Cell title = row.getCell(0);
				if(title !=null) {
					if(title.getCellType() == Cell.CELL_TYPE_STRING) {
						deal.setTitle(title.getStringCellValue());
					}
				}
				Cell content = row.getCell(1);
				if(content != null) {
					if(content.getCellType() == Cell.CELL_TYPE_STRING) {
						deal.setContent(content.getStringCellValue());
					}
				}
				Cell money = row.getCell(2);
				if(money != null) {
					if(money.getCellType() == Cell.CELL_TYPE_STRING) {
						deal.setMoney(money.getCachedFormulaResultType());
			}
				}
				Cell view = row.getCell(3);
				if(view != null) {
					if(view.getCellType() == Cell.CELL_TYPE_STRING) {
						deal.setView(view.getStringCellValue());
			}
				}
				Cell state = row.getCell(4);
				if(state != null) {
					if(state.getCellType() == Cell.CELL_TYPE_STRING) {
						deal.setState(state.getStringCellValue());
			}
				}
				save(deal,user);
				
		}
}
	private void save(Deal deal, User user) {
		deal.setView("all");
		deal.setCreatetime(DateUtil.getNow());
		deal.setUser(user);
		deal.setCreatetime("createtime");
		dealDao.save(deal);
	}
}	
	

